package com.atguigu.gulimall.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.atguigu.gulimall.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private String app_id ;

    // 商户私钥，您的PKCS8格式RSA2私钥
    private String merchant_private_key = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCQhBtZP88B7Oahpw3L4Kf0Qks5V5TgmQMVm1kYK+NfIhbr93gEf+694Hz+jL+0C9BKucF6lXnXnx8+YJ2ikx0Pnd453PL9ukY54IwX3D7Y9+VaheRqKxHlXDD1XGOLiIhpnxiLXwMi+X71ovfSWXDrUJAQfDWEtavmVplwMNQfMh/AN199rZGojtUejULqa/jN/0POV+fWZyIf0lkVUues7xBs9oHf15kqBQxYaPIBFyiln/n6/aELZ1GPMkkAqAu1j5cmowzBGe99cuxkGDhIctfSfEoIsncD3Jczet/s2ZQ5UMK3yUyP+r1GoOKOC1NrL3wp2NPb7gbVz3sYPKOVAgMBAAECggEAO5qhBQBySqMAxG94Gzx/SRkGmZHrz26MfR7jcjq3vob62Tvp/kEdoKa0JvC6KHIu6IMQr5Ry+mGgq0OCAImYlNZRz5+3gLqROFHKthSavzYPHHTYrokxCl4puyjtzaD0UkvR/bmkO4qXuIwMlm9ZSVSi9omkMSYUzh2tjz40XuDYSbXEAlHmo6lBknrzB5VictZU7tTj7VCImlbzdTnBEbTvUSsNBQAqtS7/uOMxMgM6kIebatTpkD/vPNjuhUOwXCxBSTyRfnE/fXCgR8Hba1KNVM2wd1sMetnnGhC+f6bEuGJtskxJzD2+R/cKW771Jv6LUiZRVnmKYRHUNZFMSQKBgQDRQUAt1uDTB7O6lPx+Vt9N24bqP5zZTiKlRbJXDn+phMsKtfE0A/LNRU9NeaidSPcQrAOLR6dtv3SxyieTv4gi8eIqglyE1AZG0nB4M9bV4EMriDtP3mJteVUWDDzSRpMRTf6hUElJTfbUNU1qlc1uzActmibH3XE7WC6EAbGWrwKBgQCwzJumfAegP+mBvESgQnlidE8/gFr+wHHkaw3Jp2JiUnAfCg8AKKKY45OCS4HSYy+QULkq9cgmtU60ouNQmF5/M87JRcG45npc0Ww1kBmm7XsTGAxOgPUi9k4hQCvh9zifmvVVUTSffFxYZF+zuVeWtszB8uqrScEcO/45Gzn6+wKBgQC/LTEYcB+5TWkbxvAj8PRU6oKJUxLWyfcHLfTwanOiCYuDPpSepBjZB0CblNHC2R24Ip+eSRmjP+66W+nqp2sWGg06Lx2d0SPm4m4Us6/MePaFOBKQNqmQQPDGkumiZw4IGkzPTk2WZl9io0wWW8RzPH42YIabxSmlQTxGKFH6SwKBgBQfRXveU/PeMlJsvhbLWgtll3tvvWuPNac8s/YGkgEExvQRbgGjnlXuWOSUeXMbjNdNCLYmASC+s/WFMIAEx4SMvfVUdib4PGcCAuHZw8Gm98IFk6j/rmbLR7srFZODUrN/rHqIYofgXJZG3kmTR6/EoGtWnXTfURZIkbeoLKIvAoGAJB+x7kxgQ1NBhTr3l4OcAVWcxmcfaHyKFwFDGuK6EMYNJP1mRDPF5u4LB/YejFvrHbd5woSZuj9bKqqhljwaykW28hpWF+x+Cd/ckTTB8nDEL71ZmjOdei/jxMySwsbt3GwsmAwXOozQrwNBNmqUCzqUHXknlpYbc1QhULUoQ/U=";

    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAuMrHznUPEYrmcHxSTucfrzzSNE4SNh1huwXuyh4qnkR2ASQr2hSvlAgBUvgSkzs9Q58Sm2CtXs9fYzIVoPCa9QyFbxsHkpKjusZX/DGQ3OK9UaliQe95b4fxbe3+TX7vtPaaZ1QL7zUOPQlYFEkGC6KAsuGJbvsYlM3SGXULJQcWp48JMeBNrbBT8ugT0oAdEhG0tIdLhWUpTfYAvalYF8+O1IG+s53H8JYe8GPIpngT/7t7YZp1t4ME4LBChYig1PLomDP0IZ3tX9NVub1u1X3OAuqvGo8qbOgd9o/Uv0iMejbCNzaDfI2aAC6JGULfzI5VSmNe/1phcELFGoBGNwIDAQAB";

    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    // (重要)虚拟机里面改变内网穿透地址  AlipayTemplate改地址
    private String notify_url = "http://nx5dsz.natappfree.cc/payed/notify";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private String return_url = "http://order.jiajialemall.com/memberOrder.html";

    // 签名方式
    private String sign_type = "RSA2";

    // 字符编码格式
    private String charset = "utf-8";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    //支付超时时间
    private String timeOut="30m";

    public String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();


        alipayRequest.setBizContent("{\"out_trade_no\":\"" + out_trade_no + "\","
                + "\"total_amount\":\"" + total_amount + "\","
                + "\"subject\":\"" + subject + "\","
                + "\"body\":\"" + body + "\","
                //设置过期时间为1m
                +"\"timeout_express\":\""+timeOut+"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应：" + result);

        return result;

    }
}
